﻿#ifndef _XS_VIDEO_UTILITY_H_
#define _XS_VIDEO_UTILITY_H_

#include <string>
using namespace std;
#include "opencv2/opencv.hpp"
using namespace cv;

#define CV_VERSION_ID CVAUX_STR(CV_MAJOR_VERSION) CVAUX_STR(CV_MINOR_VERSION) CVAUX_STR(CV_SUBMINOR_VERSION)
#ifdef _DEBUG
#define cvLIB(name) "opencv_" name CV_VERSION_ID "d"
#else
#define cvLIB(name) "opencv_" name CV_VERSION_ID
#endif

#pragma comment( lib, cvLIB("core"))
#pragma comment( lib, cvLIB("imgproc"))
#pragma comment( lib, cvLIB("highgui"))

/** 需要以下 dll：
*   opencv_core###d.dll
*   opencv_highgui###d.dll
*   opencv_imgproc###d.dll
*   opencv_ffmpeg###.dll
*   opencv_video###d.dll
*/

/** 使用示例
 * VideoR src_video;
 * VideoCtl video_ctl;
 * 
 * // 绑定控件
 * CWnd *video_wnd = GetDlgItem(IDC_VIDEO);
 * video_ctl.bind_ctl(video_wnd);
 * 
 * //打开视频
 * src_video.open(s_video_file);
 * 
 * video_ctl.clear_wnd();
 * 
 * cv::Mat cur_frame;
 * while(src_video.get_next_frame(cur_frame)){// 获取下一帧
 *     // 显示图像
 *     video_ctl.show_image(cur_frame);
 * }
 */

// video reader
class VideoR
{
public:
    VideoR();
    ~VideoR();

private:
    VideoCapture cap;

public:
    // open a web camera of a video file
    bool open(const string& _video_uri);
    // open a capture device
    bool open(int device);

    bool is_opened();
    void close();

public:
    bool get_next_frame(Mat& _img);
    bool get_frame(Mat& _img, unsigned int _frame_num=-1);// get the _frame_num frame, next by default

public:
    unsigned int get_frame_num();// next frame number
};

// 视频控件
class VideoCtl
{
public:
    VideoCtl();
    VideoCtl(CWnd* _ctl, int flags = WINDOW_AUTOSIZE);

private:
    static int call_cnt;
    static const string wnd_name_pre;
    string wnd_name;

private:
    CWnd* ctl;
    CRect wnd_rect;

public:
    void bind_ctl(CWnd* _ctl, int flags = CV_WINDOW_AUTOSIZE);// 绑定控件

public:
    void show_image(const Mat& img, bool resize=true);
    void clear_wnd();
};
#endif
